import * as Koa from 'koa';
// import * as bodyParser from 'koa-bodyparser';
// tslint:disable:no-var-requires
const koaBody = require('koa-body');
import * as koaRequest from 'koa-http-request';
import * as cors from 'koa2-cors';
// import * as ecRouter from 'ec-router';
const log4js = require('log4js');
log4js.configure('./src/logger.json');

import 'reflect-metadata';
import { Config } from './app.config';
import { Router } from './router';
import { StringUtils } from './utility/string.utility';

const app = new Koa();
app.proxy = true;
app.use(cors());
app.use(koaBody({
    multipart: true, // 支持文件上传
    // encoding: 'gzip',
    // tslint:disable-next-line:object-literal-sort-keys
    formidable: {
        keepExtensions: true,    // 保持文件的后缀
        maxFileSize: 10 * 1024 * 1024 * 1024, // 文件上传大小
        onFileBegin: (name, file) => { // 文件上传前的设置
        },
        uploadDir: Config.UPLOAD_PATH, // 设置文件上传目录
    },
}));
app.use(koaRequest({ timeout: 15000000 }));
app.use(Router.routes());
app.use(Router.allowedMethods());
// ecRouter.loadConfig(__dirname + '/src/router.config.js');
// app.use(ecRouter.dispatcher());

const server = app.listen(Config.PORT);
console.log(StringUtils.format('app started at port {0}...', Config.PORT + ''));
server.setTimeout(0);
